package com.wang.xml;

import java.io.File;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;

/**
 * 使用JDOM操作XML（可扩展标记语言）。
 * @author wangmin
 * @since 2017年4月6日-下午1:51:07
 * @version V1.0
 */
public class JDOMReaderXml {

	@SuppressWarnings("rawtypes")
	public static void  main(String[] args)
	{
		try
		{
			URL url = JDOMReaderXml.class.getResource("/img_391d954c_19.xml");
			SAXBuilder builder = new SAXBuilder();
			Document doc = builder.build(new File(url.getFile()));
			//获取根节点
			Element ele = doc.getRootElement();
			List list = ele.getChildren("object");
			Map<String,String > result = new HashMap<>();
			for (int i = 0 ; i < list.size() ; i++)
			{
				System.out.println("name：" + ((Element)list.get(i)).getChild("name").getText());
				result.put("name",((Element)list.get(i)).getChild("name").getText());
			}
			List list2 = ele.getChildren("lonAndLatRange");
			for (int j = 0; j < list2.size(); j++) {
				System.out.println("startX：" + ((Element) list2.get(j)).getChild("startX").getText());
				System.out.println("endX：" + ((Element) list2.get(j)).getChild("endX").getText());
				System.out.println("startY：" + ((Element) list2.get(j)).getChild("startY").getText());
				System.out.println("endY：" + ((Element) list2.get(j)).getChild("endY").getText());
				result.put("startX", ((Element) list2.get(j)).getChild("startX").getText());
				result.put("endX",((Element) list2.get(j)).getChild("endX").getText());
				result.put("startY",((Element) list2.get(j)).getChild("startY").getText());
				result.put("endY",((Element) list2.get(j)).getChild("endY").getText());

			}

			System.out.println(result);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}

	}
}
